#include <stdlib.h>
#include <stdio.h>

#include <windows.h>

#include "lib.h"
#include "kenoserv.h"
#include "log.h"
#include "logger.h"
#include "getrand.h"
#include "casbase.h"

#include "stdolog.h"

static KenoServer* serv = (KenoServer*)0;

static BOOL __stdcall ctrlHandler(DWORD);

void main(int c, char *v[])
{
	int port = 2062;

	if (c == 2)
		port = atoi(v[1]);

	SetConsoleCtrlHandler(ctrlHandler, TRUE);

	CasinoBase::load();
	port += CasinoBase::baseport;

	if (CasinoBase::baseport)
		printf("\n*** port adjusted by %d:  listening on %d\n",
			CasinoBase::baseport, port);
	else
		printf("\n*** listening on %d\n", port);

	Log::init(new StdoutLogger());
	serv = new KenoServer(port);
	if (serv)
		serv->run();
	delete serv;
	Log::uninit();
}

uint32 getrand()
{
	return serv->rand();
}

BOOL __stdcall ctrlHandler(DWORD event)
{
	static BOOL shuttingDown = FALSE;

	if (!shuttingDown)
	{
		shuttingDown = TRUE;

		if (serv)
			serv->stop();
	}
	return TRUE;
}
